17. Matrix Functions
Matrix Functions
The last part of the Matrix class involves implementing the matrix functionality. You are welcome to program as many matrix operations as you'd like: addition, multiplication, transpose, inverse, etc.
We recommend at least implementing matrix addition and a function called matrix_print that outputs the matrix to the terminal using cout. In the solution given at the end of this page, we've also provided code for a matrix_transpose function.
Implementing these class functions is the same as implementing the get and set functions from the previous part of the lesson; you will need to declare your functions in matrix.h and define your functions in matrix.cpp. The general syntax is the same:
class function declaration syntax
output_datatype functionname(datatype variable1,
datatype variable2, ..., datatype variablen)
class function definition syntax
output_datatype Classname::functionname(datatype variable1,
datatype variable2, ..., datatype variablen) {
code defining the function;
}
Writing the Matrix Functions
In this exercise, you will declare and define a Matrix class function that adds two matrices together. Here are the inputs and outputs of the matrix addition function:
INPUTS:
- a matrix, which will be added to the grid variable
OUTPUTS
- a matrix containing the sum of the grid variable matrix and input matrix
Because the input to the matrix_addition function is a Matrix, you will need to declare and define your function using the Matrix class as the data type. This might seem a bit confusing, but the Gaussian class presented earlier in the lesson did the same thing with the mul and add functions. You can use those as a guide for writing your matrix_addition functions.
As a reminder, here are the function declarations for the mul and add functions in gaussian.h:
Gaussian mul (Gaussian);
Gaussian add (Gaussian);
Both of these functions receive a Gaussian and output a Gaussian. Here are the function definitions from gaussian.cpp:
Gaussian Gaussian::mul(Gaussian other) {
float denominator;
float numerator;
float new_mu;
float new_var;
denominator = sigma2 + other.getSigma2();
numerator = mu * other.getSigma2() + other.getMu() * sigma2;
new_mu = numerator / denominator;
new_var = 1.0 / ( (1.0 / sigma2) + (1.0 / other.sigma2) );
return Gaussian(new_mu, new_var);
}
Gaussian Gaussian::add(Gaussian other) {
float new_mu;
float new_sigma2;
new_mu = mu + other.getMu();
new_sigma2 = sigma2 + other.getSigma2();
return Gaussian(new_mu, new_sigma2);
}
Although the implementation of the matrix_addition function will be different, the general structure will be the same as the mul and add functions from the Gaussian example.
You will also write a matrix_print function that outputs a matrix to the terminal using cout. The matrix_print function has no inputs and no outputs.
Fill out the TODOS in the matrix.cpp and matrix.h code.
Start Quiz:
#include <iostream>
#include <vector>
#include "matrix.h"
int main () {
// TODO: Nothing to do here
return 0;
}
#include "matrix.h"
Matrix::Matrix() {
std::vector <std:: vector <float> > initial_grid (10, std::vector <float>(5, 0.5));
grid = initial_grid;
rows = initial_grid.size();
cols = initial_grid[0].size();
}
Matrix::Matrix(std::vector <std:: vector <float> > initial_grid) {
grid = initial_grid;
rows = initial_grid.size();
cols = initial_grid[0].size();
}
void Matrix::setGrid(std::vector< std::vector<float> > new_grid) {
grid = new_grid;
rows = new_grid.size();
cols = new_grid[0].size();
}
std::vector< std::vector<float> > Matrix::getGrid() {
return grid;
}
std::vector<int>::size_type Matrix::getRows() {
return rows;
}
std::vector<int>::size_type Matrix::getCols() {
return cols;
}
/* TODO: Define a matrix_addition function
** INPUT: a matrix
** OUPUT: the sum of the grid variable and the input matrix
**
** STEPS:
** 1. check that the matrix in the grid variable
** and the input matrix have the same size
**
** if not, throw an error like
** throw std::invalid_argument("matrices are not the same size");
**
** 2. add the matrices together and return
** the result as a Matrix. You can do this part
** with nested for loops. If you use an intermediate
** vector to store a row, the vector.clear()
** method might be useful.
**
*/
/* TODO: Define a matrix_print function
** This function has no inputs and no outputs
** The purpose of the function is to display the matrix in
** the terminal using std::cout.
**
**
** The output should look something like:
** 4 9 1 10
** 5 11 6 17
** 8 4 15 2
*/
#include <vector>
#include <stdexcept> //library for the invalid_argument method
#include <iostream>
class Matrix
{
private:
std::vector< std::vector<float> > grid;
std::vector<float>::size_type rows;
std::vector<float>::size_type cols;
public:
// constructor functions
Matrix ();
Matrix (std::vector< std::vector<float> >);
// set functions
void setGrid(std::vector< std::vector<float> >);
// get functions
std::vector< std::vector<float> > getGrid();
std::vector<float>::size_type getRows();
std::vector<float>::size_type getCols();
/* TODO: Declare the matrix_addition function
** INPUTS: a Matrix
** OUTPUTS: a Matrix
**
** TODO: Declare the matrix_print function
** INPUTS: none
** OUTPUTS: none
*/
};
Solution matrix.h
#include <vector>
#include <iostream>
#include <stdexcept>
class Matrix
{
private:
std::vector< std::vector<float> > grid;
std::vector<float>::size_type rows;
std::vector<float>::size_type cols;
public:
// constructor functions
Matrix ();
Matrix (std::vector< std::vector<float> >);
// set functions
void setGrid(std::vector< std::vector<float> >);
// get functions
std::vector< std::vector<float> > getGrid();
std::vector<float>::size_type getRows();
std::vector<float>::size_type getCols();
// matrix functions
Matrix matrix_transpose();
Matrix matrix_addition(Matrix);
// matrix printing
void matrix_print();
};
Solution matrix.cpp
#include "matrix.h"
Matrix::Matrix() {
std::vector <std:: vector <float> > initial_grid (10, std::vector <float>(5, 0.5));
grid = initial_grid;
rows = initial_grid.size();
cols = initial_grid[0].size();
}
Matrix::Matrix(std::vector <std:: vector <float> > initial_grid) {
grid = initial_grid;
rows = initial_grid.size();
cols = initial_grid[0].size();
}
void Matrix::setGrid(std::vector< std::vector<float> > new_grid) {
grid = new_grid;
rows = new_grid.size();
cols = new_grid[0].size();
}
std::vector< std::vector<float> > Matrix::getGrid() {
return grid;
}
std::vector<float>::size_type Matrix::getRows() {
return rows;
}
std::vector<float>::size_type Matrix::getCols() {
return cols;
}
Matrix Matrix::matrix_transpose() {
std::vector< std::vector<float> > new_grid;
std::vector<float> row;
for (int i = 0; i < cols; i++) {
row.clear();
for (int j = 0; j < rows; j++) {
row.push_back(grid[j][i]);
}
new_grid.push_back(row);
}
return Matrix(new_grid);
}
Matrix Matrix::matrix_addition(Matrix other) {
if ((rows != other.getRows()) || (cols != other.getCols())) {
throw std::invalid_argument( "matrices are not the same size" );
}
std::vector< std::vector<float> > othergrid = other.getGrid();
std::vector< std::vector<float> > result;
std::vector<float> new_row;
for (int i = 0; i < rows; i++) {
new_row.clear();
for (int j = 0; j < cols; j++) {
new_row.push_back(grid[i][j] + othergrid[i][j]);
}
result.push_back(new_row);
}
return Matrix(result);
}
void Matrix::matrix_print() {
std::cout << std::endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
std::cout << grid[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}